package benders_decomposition;

/*---------------------------------------------------------

Original Problem:

    min 7(y1 + y2 + y3 + y4 + y5) + x1 + x2 + x3 + x4 + x5

    s.t.    x1 + x4 + x5 = 8
            x2 + x5 = 3
            x3 + x4 = 5
            x1 <= 8y1
            x2 <= 3y2
            x3 <= 5y3
            x4 <= 5y4
            x5 <= 3y5
            x >= 0
            y = 0 or 1

---------------------------------------------------------*/

import gurobi.GRB;

public class BendersDecomposition {
    static int i = 0;

    public static void main(String[] args) {

        MasterProblem mp = new MasterProblem();

        while (true) {

            // 求解主问题
            mp.solve();

            if (i == 3) {
                System.out.println("1");
            }

            // 将y传入对偶子问题并求解
            DualSubproblem ds = new DualSubproblem(mp.solutions);
            ds.solve();

            if (ds.status == GRB.Status.OPTIMAL) {
                if (ds.objVal == mp.tValue) {
                    break;
                } else {
                    mp.addOptimalityCut(ds.solutions);
                }
            } else if (ds.status == GRB.Status.UNBOUNDED) {
                mp.addFeasibilityCut(ds.solutions);
            } else {
                System.out.println(ds.status);
                break;
            }
            i++;

        }

        // 输出最优解
        System.out.println("最优值为: " + mp.objVal);
    }
}
